﻿using Atlanchim.Crm.Mui.Commands;
using Atlanchim.Crm.Mui.Helpers;
using FirstFloor.ModernUI.Windows.Controls;
using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui.Pages.Telephones
{
    public class PageViewTelephoneModel : AbstractPageViewModel
    {
        /// <summary>
        /// Contact de référence
        /// </summary>
        public Telephone Telephone { get; set; }

        /// <summary>
        /// Url d'accès à la page d'édition
        /// </summary>
        public override string EditUrl { get; set; }

        /// <summary>
        /// Chargement
        /// </summary>
        public override void Load()
        {
            // Lecture du fragment
            int idTelephone = 0;
            int.TryParse(this.DicFragments["IdTelephone"], out idTelephone);

            // Chargement
            using (CrmContext context = new CrmContext())
            {
                // Contact
                this.Telephone = context
                                .Telephones
                                .Where(tel => tel.Id == idTelephone)
                                .FirstOrDefault();
                this.EditUrl = string.Format("/Pages/Telephones/PageEditTelephone.xaml#IdTelephone={0}", idTelephone);
            }

            // Notification
            OnPropertyChanged("Telephone");
        }

        /// <summary>
        /// Suppression
        /// </summary>
        protected override void OnDeleteCurrentEntity()
        {
            CrmCrudHelper.DeleteEntityNewContext(this.Telephone);
        }
    }
}
